﻿Imports System.Drawing, System.Threading, System.Windows.Forms, EnvDTE


''' <summary>
''' 
''' </summary>
''' <remarks></remarks>
''' <features></features>
''' <stepthrough>False</stepthrough>
<System.ComponentModel.Description("Add Something Later")>
Public Class ResultControl
    Inherits UserControl

#Region "       Properties          >>>"

    Private Const MaxLoadResultSeconds As Integer = 60
    Private Property HasLoadedResults As Boolean

    Public Property AfterSelect As Action(Of System.Windows.Forms.TreeNode)
        Get
            Return Me.treeListViewControl.AfterClick
        End Get
        Set(ByVal value As Action(Of System.Windows.Forms.TreeNode))
            Me.treeListViewControl.AfterClick = value
        End Set
    End Property

    '<System.ComponentModel.Category("Action")>
    'Public Event AfterSelect(ByVal Work As Action(Of System.Windows.Forms.TreeNode))

#End Region


#Region "       Static              >>>"
    <DebuggerStepThrough()>
    Public Sub New()
        InitializeComponent()
        OnAutoResizeColumnHeaders() '<-- Do I need this? Check!
    End Sub

    ''' <summary>
    ''' Can I InLine this into the OnResize Event
    ''' </summary>
    ''' <remarks></remarks>
    ''' <stepthrough>Enabled</stepthrough>
    <DebuggerNonUserCode()>
    Private Sub OnAutoResizeColumnHeaders()
        'Dim factor As Double = CDbl(Me.Width - 20) / CDbl(treeListViewControl.TotalColWidth)
        Dim factor As Double = (Me.Width - 20) / treeListViewControl.TotalColWidth
        For Each col In treeListViewControl.Columns.Cast(Of ColumnHeader)()
            col.Width = CInt(Math.Truncate(CDbl(col.Width) * factor))
        Next
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    ''' <stepthrough></stepthrough>
    Protected Overrides Sub OnResize(ByVal e As EventArgs)
        MyBase.OnResize(e)
        toolStrip1.Width = Me.Width '<-- Do I Need This?
        OnAutoResizeColumnHeaders() '<-- Do I Need This?
    End Sub


    ''' <summary>
    ''' What is this Used For?
    ''' </summary>
    ''' <param name="message"></param>
    ''' <remarks></remarks>
    ''' <stepthrough></stepthrough>
    Friend Sub TellSingleMessage(ByVal message As String)
        singleResultPanel.BringToFront()
        singleResultPanel.Visible = True
        lblResultMessage.Text = message
    End Sub



#End Region


#Region "       Search/Filter       >>>"

    Private Sub filterTextToolStripTextBox_Click(ByVal sender As Object, ByVal e As EventArgs) Handles filterTextToolStripTextBox.Click
    End Sub

    Private Sub filterTextToolStripTextBox_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles filterTextToolStripTextBox.TextChanged
        If HasLoadedResults Then
            resultSearchRunNr += 1
            tmrResultSearch.Start()
        End If
    End Sub


    Private resultSearchRunNr As Integer = 0
    Private Sub tmrResultSearch_Tick(ByVal sender As Object, ByVal e As EventArgs)
        Dim runNr As Integer = resultSearchRunNr
        tmrResultSearch.[Stop]()
        Dim searchText As String = filterTextToolStripTextBox.Text
        Dim treeView As TreeView = treeListViewControl.TreeView
        Dim baseTreeNode As TreeNode = TryCast(treeView.Tag, TreeNode)
        If searchText = "" Then
            treeView.Nodes.Clear()
            treeView.Nodes.Add(baseTreeNode)
            treeView.ShowPlusMinus = True
        Else
            treeView.Nodes.Clear()
            Dim allNodesCompleteList = baseTreeNode.GetRecursive(New List(Of TreeNode)(), Function(tn, list) tn.Nodes.Cast(Of TreeNode)())
            Dim allNodes = allNodesCompleteList.Where(Function(t) t.Text.ToLower().Contains(searchText.ToLower()))
            treeView.ShowPlusMinus = False
            For Each tn In allNodes
                If resultSearchRunNr <> runNr Then
                    Exit For
                End If
                Dim origin As String = TryCast(tn.Tag, Expression).Name
                Dim tmpTn = tn
                While tmpTn.Parent IsNot Nothing
                    If tmpTn.Parent.Parent Is Nothing Then
                        origin = "result." & origin
                        Exit While
                    End If
                    Dim name As String = TryCast(tmpTn.Parent.Tag, Expression).Name
                    If name.Contains("{") Then
                        name = name.Substring(name.IndexOf("{") - 1).Trim()
                    End If
                    origin = name & "." & origin
                    tmpTn = tmpTn.Parent
                    If resultSearchRunNr <> runNr Then
                        Exit While
                    End If
                End While
                If resultSearchRunNr <> runNr Then
                    Exit For
                End If
                treeView.Nodes.Add(Convert.ToString(tn.Text) & origin)
                Application.DoEvents()
            Next
        End If
    End Sub

#End Region




    ''' <summary>
    ''' Try to Make it so the ToolWindow can do this Directly
    ''' BackLoad the Node Creation into the TreeView Directly
    ''' </summary>
    ''' <param name="expression"></param>
    ''' <remarks></remarks>
    ''' <stepthrough></stepthrough>
    Friend Sub DisplayExpression(ByVal expression As Object)
        singleResultPanel.SendToBack()
        singleResultPanel.Visible = False
        treeListViewControl.TreeView.Nodes.Clear()

        Dim mainNodeText As String = String.Empty
        If expression Is Nothing Then
            mainNodeText = treeListViewControl.CreateMultiColumnNodeText("result", "null", "Nothing", "")
        Else
            mainNodeText = treeListViewControl.CreateMultiColumnNodeText("result", expression.ToString, expression.GetType.Name, "")

            Dim tn As New TreeNode(mainNodeText) With {.Tag = expression}
            treeListViewControl.TreeView.Nodes.Add(tn)
            treeListViewControl.TreeView.Tag = tn

            If TypeOf expression Is IEnumerable Then
                For Each Item In DirectCast(expression, IEnumerable)
                    Dim Text As String = treeListViewControl.CreateMultiColumnNodeText(Item.GetType.Name, Item.ToString, Item.GetType.Name, "")
                    tn.Nodes.Add(New TreeNode(Text) With {.Tag = Item})
                Next
            End If
            treeListViewControl.Enabled = True
        End If

        HasLoadedResults = True
    End Sub




End Class